package academic.ui.client.view;


import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Image;

import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;
import ui.client.widgets.DataDefinition;

import academic.ui.client.config.CSSConstants;
import academic.ui.client.place.Tokens;
import academic.ui.client.presenter.ProductPresenter;
import academic.ui.client.util.ClientHelper;
import academic.ui.client.widgets.CompactDeals;
import academic.ui.client.widgets.DiscussionWidget;
import academic.ui.client.widgets.FadePanel;
import academic.ui.client.widgets.SliderPanel;
import academic.ui.client.widgets.TimerDiv;
import ecommerce.soco.shared.GWT_Discussion;
import ecommerce.soco.shared.GWT_Merchant;
import ecommerce.soco.shared.GWT_Product;

public class ProductView extends ViewImpl implements ProductPresenter.MyView {

	public FadePanel fader = new FadePanel();
	public SliderPanel slider = new SliderPanel();
	private GenericContainerTag div = new GenericContainerTag("div");
	private GenericClickable buyButton = new GenericClickable("a");
	private GenericClickable joinDiscussionLink = new GenericClickable("a","talk about this deal!");
	private GenericContainerTag discussionContent = new GenericContainerTag("div");
	private Boolean isExpired=false;
	
	private ArrayList<GenericClickable> clickHandlers;
	
	@Inject
	public ProductView(CSSConstants css) {
		slider.setStyleName("productFull");
		slider.setAnimationEnabled(true);
		slider.setContent(div);
		fader.setContent(slider);
		fader.setAnimationEnabled(true);
		fader.setStyleName("productView Widget");
		joinDiscussionLink.setStyleName("joinDiscussionLink");
		buyButton.clear();
		buyButton.setStyleName("buyButton");
		Image img = new Image(GWT.getModuleBaseURL()+"images/buy.png");
		buyButton.add(img);
	}
	
	@Override
	public Widget asWidget() {
		return fader;
	}
	
	@Override
	public ArrayList<GenericClickable> getClickHandlers() {
		return clickHandlers;
	}

	@Override
	public void setup(ArrayList<GWT_Product> products) {
		setup(products.get(products.size()-1));

		// Add the other two products
		CompactDeals cd = new CompactDeals(products.subList(0, products.size()-1));
		clickHandlers = cd.getClickHandlers();
		cd.displayHeader(false);
		div.add(cd);
		discussionContent.clear();
		div.add(discussionContent);
		discussionContent.add(discussionWidget);
	}
	
	@Override
	public HasClickHandlers buyButton() {
		return buyButton;
	}
	
	@Override
	public HasClickHandlers joinDiscussionLink() {
		return joinDiscussionLink;
	}


	@Override
	public void hide() {
		slider.setOpen(false);
	}

	@Override
	public void fadeIn() {
		fader.fadeIn(500);
	}

	@Override
	public void fadeOut() {
		fader.fadeOut();
	}

	@Override
	public void setExpired(Boolean expired) {
		isExpired=expired;
	}

	@Override
	public boolean isExpired() {
		return isExpired;
	}

	@Override
	public void setup(GWT_Product product) {
		div.clear();
		div.setStyleName("outerContent");
		GenericContainerTag content = new GenericContainerTag("div");
		content.setStyleName("content");
		div.add(content);
		GenericContainerTag merchantContent = new GenericContainerTag("div");
		div.add(merchantContent);
		
		discussionContent.setStyleName("content");
		
		merchantContent.setStyleName("content");
		GenericContainerTag h2 = new GenericContainerTag("h2",product.getName());
		content.add(h2);
		
		//Primary div contains the price,value,time left etc.
		GenericContainerTag primary = new GenericContainerTag("div");
		primary.setStyleName("primary");
		GenericContainerTag priceTag = new GenericContainerTag("div");
		priceTag.setStyleName("priceTag");
		priceTag.add(new Image(GWT.getModuleBaseURL()+"images/price_tag.png"));
		primary.add(priceTag);
		
		GenericContainerTag price = new GenericContainerTag("div");
		price.setStyleName("price");
		GenericContainerTag amount = new GenericContainerTag("div",ClientHelper.formatPrice(product.getPrice0()));
		amount.setStyleName("amount");
		
		primary.add(price);
		price.add(amount);
		
		GenericContainerTag buyDiv = new GenericContainerTag("div");
		GenericContainerTag disabledDiv = new GenericContainerTag("div","No Longer Available");
		disabledDiv.setStyleName("disabled");
		if(!isExpired)
			buyDiv.add(buyButton);
		else
			buyDiv.add(disabledDiv);
		GenericContainerTag divBuy = new GenericContainerTag("div");
		divBuy.setStyleName("buy");
		
		GenericContainerTag value = new GenericContainerTag("div");
		value.setStyleName("value");
		
		value.add(new DataDefinition("Value",ClientHelper.formatPrice(product.getPrice1())));
		
		DataDefinition discount=new DataDefinition("Discount",product.discount(),"discount");
		
		DataDefinition save = new DataDefinition("Savings",ClientHelper.formatPrice(product.getPrice1()-product.getPrice0()),"save");
		
		value.add(discount);
		value.add(save);
		divBuy.add(value);
		buyDiv.add(divBuy);
		
		primary.add(buyDiv);
		content.add(primary);
		
		//Secondary Contains the description and the photos
		GenericContainerTag secondary = new GenericContainerTag("div");
		secondary.setStyleName("secondary");
		
		GenericContainerTag photos = new GenericContainerTag("div");
		photos.setStyleName("photos");
		GenericContainerTag ul = new GenericContainerTag("ul");
		GenericContainerTag li = new GenericContainerTag("li");
		li.add(ClientHelper.createImage(GWT.getModuleBaseURL()+"../PICS/Products/"+product.getImage(),product.getImgRatio(),400));
		photos.add(ul);
		ul.add(li);
		
		secondary.add(photos);
		
		GenericContainerTag finePrint = new GenericContainerTag("div");
		finePrint.setStyleName("fine_print");
		GenericContainerTag h3 = new GenericContainerTag("h3","Details");
		GenericContainerTag ul1 = new GenericContainerTag("ul");
		GenericContainerTag li1 = new GenericContainerTag("li");
		li1.add(new GenericContainerTag("div",product.getDescription()));
		finePrint.add(h3);
		finePrint.add(ul1);
		ul1.add(li1);
		secondary.add(finePrint);
		
		GenericContainerTag shareDealDiv = new GenericContainerTag("div");
		shareDealDiv.setStyleName("shareDealDiv");
		
		GenericClickable twitterShare = new GenericClickable("a");
		GenericClickable emailShare = new GenericClickable("a");
		GenericContainerTag faceLikeDiv = new GenericContainerTag("div");
		faceLikeDiv.setStyleName("faceLikeDiv");
		GenericClickable faceBookShare = new GenericClickable("fb:like");
		GenericContainerTag faceBookScript = new GenericContainerTag("script");
		faceLikeDiv.add(faceBookScript);
		faceBookScript.getElement().setAttribute("src","http://connect.facebook.net/en_US/all.js#xfbml=1");
		faceBookShare.getElement().setAttribute("href", GWT.getModuleBaseURL()+Tokens.productFull+";id="+product.getId());
		faceBookShare.getElement().setAttribute("layout", "button_count");
		faceBookShare.getElement().setAttribute("colorscheme", "dark");
		faceLikeDiv.add(faceBookShare);
		
		twitterShare.setStyleName("twitterShare");
		emailShare.setStyleName("emailShare");
		
		shareDealDiv.add(twitterShare);
		shareDealDiv.add(emailShare);
		shareDealDiv.add(faceLikeDiv);
		
		content.add(secondary);
		content.add(shareDealDiv);
		if(!isExpired)
		{
			TimerDiv timerDiv = new TimerDiv(product.getPurchaseExpiryDate());
			GenericContainerTag timerWrapper = new GenericContainerTag("div");
			timerWrapper.setStyleName("timerWrapper");
			timerWrapper.add(timerDiv);
			content.add(timerWrapper);
		}
		
		
		//add all merchant content
		GWT_Merchant merchant = product.getMerchant();
		GenericContainerTag merchantName = new GenericContainerTag("a",merchant.getName());
		GenericContainerTag merchantAddress = new GenericContainerTag("a",merchant.getAddress());
		GenericContainerTag merchantWebsite = new GenericContainerTag("a","website");
		merchantWebsite.getElement().setAttribute("href", merchant.getUrl());
		merchantWebsite.getElement().setAttribute("target", "_blank");
		Image merchantImg = new Image(GWT.getModuleBaseURL()+"../PICS/Merchants/"+merchant.getImage());
		merchantImg.setStyleName("companyImg");
		GenericClickable merchantMapLink = new GenericClickable("a","map");
		merchantMapLink.getElement().setAttribute("href", "http://mapof.it/"+merchant.getMapKeywords());
		merchantMapLink.getElement().setAttribute("target","_blank");
		GenericContainerTag merchantInfoDiv = new GenericContainerTag("div");
		merchantInfoDiv.setStyleName("merchantInfoDiv");
		merchantInfoDiv.add(new GenericContainerTag("h3","Company Overview"));
		merchantContent.add(merchantInfoDiv);
		merchantInfoDiv.add(merchantName);
		merchantInfoDiv.add(merchantAddress);
		merchantInfoDiv.add(merchantWebsite);
		merchantInfoDiv.add(merchantImg);
		merchantInfoDiv.add(merchantMapLink);
		slider.setOpen(true);
	}
	
	DiscussionWidget discussionWidget = new DiscussionWidget();
	
	@Override
	public void setupDiscussionContent(GWT_Discussion discussion,Boolean isLoggedIn) {
		discussionWidget.setupMessages(discussion, isLoggedIn);
	}
	
	@Override
	public DiscussionWidget getDiscussionWidget() {
		return discussionWidget;
	}

}
